/*
	Copyright (C) 2010 Tim Beyer
	
    This file is part of Pixelbots.

    Pixelbots is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Pixelbots is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Pixelbots.  If not, see <http://www.gnu.org/licenses/>.
	
*/

function startUp(){
		//add agent types
		agentTypes.push(BrightBreeder);		
		agentTypes.push(BrightSeeker);
		agentTypes.push(RandomGrowth);	
		//set default agent
		agent = agentTypes[0];
		
		img.onload = function(){ //initialize program when image is loaded
			init();
		};	
		
		//fill the default images
		defaultImages.push("images/henning.jpg");		
		defaultImages.push("images/stairs.jpg");		
		defaultImages.push("images/fire2.jpg");
		
		
		//load first image
		img.src = defaultImages[0];
}



function init(){
	
	//set dimensions
	width = img.width;
	height = img.height;
	
	//create canvas and context
	canvas = document.getElementById("can");
	ctx = canvas.getContext("2d");
	
	//set canvas dimensions
	canvas.width = width;
	canvas.height = height;
	
	//create new empty pixel image
	originalImage = new PixelImage(1,1);
	//load the image
	originalImage.loadImage(img);
	//Draw
	originalImage.drawToCanvas(ctx,0,0);
	//next part of initialization
	//initAgents(agent);
	initUI();
}

function initAgents(agentType){

	//Create the new pixel image to be drawn on
	displayImage = new PixelImage(originalImage.width,originalImage.height);	
	//initialize agent array
	agents = new Array();
	//fetch brightness map 
	var bmap = new BrightnessMap(originalImage);
	//fill the array
	for(var i = 0; i < numberAgents; i++){	
		var x = random(bmap.width);
		var y = random(bmap.height);
		var agent = new agentType(bmap,x,y);
		//add
		agents.push(agent);
	}
	
	//next init step
			
}


function reinit(){
	initDone = false;
	init();			
}

function start(){
	//initialize agents
	initAgents(agent);
	if(!initDone) return; //cannot start when not initialized
	running = true;
	animate();	
}

function pause(){
	if(running && !paused){
		running = false;
		paused = true;
	}
	else if(!running && paused){
		paused = false;
		start();
	}
}

function stop(){
	running = false;
	reinit();
}

function animate(){

	//wait until init is complete
	if(!running) return;

		for(var i = 0; i < agents.length; i++){
			var agent = agents[i];
			agent.nextStep();
			agent.draw(displayImage);
		}
		displayImage.drawToCanvas(ctx,0,0);
		setTimeout("animate();",30);
	
}


function handleFiles(files){
	if(files.length > 1){
		alert("Only one file can be used");
		return;
	}
	if(files.length == 0){
		alert("No file selected");
		return;
	}

	var file = files[0];  
	var imageType = /image.*/;  
	  
	if (!file.type.match(imageType)) {  
		return;
	}  
	  
	img.onload = function(){ //add onload listener
		init();
	};		
	  
	var reader = new FileReader();  
	reader.onload = (function(e) { img.src = e.target.result; });  
	reader.readAsDataURL(file); 
}


	

function loadAgent(number){
	agent = agentTypes[number];
	if(number == 0) numberAgents = 1; // Has all agents initialized internally
	if(number == 1) numberAgents = 10000;
	if(number == 2) numberAgents = 4;
	stop();				
}

function loadImage(number){
	//stop animation
	running = false;
	var image = defaultImages[number];
	img.onload = function(){ //initialize program when image is loaded
		init();
	};	
	img.src = image;	
}

function changeAgentCount(number){
	numberAgents = number;
	stop();
}


function inCanvas(x,y){
	//get canvas position
	var canvasPos = $('#can').position();
	var left = canvasPos.left;
	var top = canvasPos.top;
	
	return(x > left && x < left + width && y > top && y < top + height);
}
